﻿var app = angular.module("planet", ['ngRoute']);
app.config(function ($routeProvider) {
    $routeProvider.when("/", {
        templateUrl: "Views/home.html",
        controller: "mainController"
    }).when("/Class/:class", {
        templateUrl: "Views/home.html",
        controller: "mainController"
    }).when("/Add", {
        templateUrl: "Views/addPlanet.html",
        controller: "addPlanetController"
    }).when("/Search", {
        templateUrl: "Views/listPlanet.html",
        controller: "listPlanetController"
    }).when("/:id", {
        templateUrl: "Views/home.html",
        controller: "mainController"
    }).otherwise ({
        templateUrl: "Views/error.html"
    });
});
app.factory("Planets", function () {
    return [];
});
app.factory("Class", function() {
    return [{name: "Liquid", link: "Liquid"},
            {name: "Solid", link: "Solid"},
            {name: "Gas", link: "Gas"}];

});
app.filter("reverse", function () {
    return function (item) {
        item.split("").reverse().join("");
    };
});
app.controller("navController",
    function ($scope, Planets, Class) {
        $scope.navs = Class;
    });
app.controller("mainController", function ($scope, $http, Planets, $routeParams){ $scope.planets = Planets;
    $scope.class = "All Planets";
    if ($routeParams.class) {
        $scope.class = $routeParams.class;
    }
    if ($routeParams.id) {
        $scope.currentIndex = $routeParams.id;
    }
    else {
        $scope.currentIndex = 0;
    }
    $scope.params = $routeParams;
    $scope.currentPlanet = Planets[$scope.currentIndex];
    $scope.next = function () {
        $scope.currentIndex++;
        if ($scope.currentIndex > Planets.length - 1) {
            $scope.currentIndex = 0;
        }
        $scope.currentPlanet = Planets[$scope.currentIndex];
    };
    $scope.prev = function () {
        $scope.currentIndex--;
        if ($scope.currentIndex < 0) {
            $scope.currentIndex = Planets.length - 1;
        }
        $scope.currentPlanet = Planets[$scope.currentIndex];
    };
    $scope.notify = function () {} ;
    $http.get("https://9er.firebaseio.com/planets/.json"
        ).success(
        function (data) {
            while (Planets.length){
                Planets.pop();
            }
            for (var x in data) {
                data[x].key = x;
                Planets.push(data[x]);
            }
            $scope.currentPlanet = Planets[$scope.currentIndex];
        }
        ).error(
        function () {
            console.log("Error on Get");
        });
});
app.controller("addPlanetController", function ($scope, $http, Planets, Class, $location) {
    $scope.class = Class;
    $scope.savePlanet = function (planet) {
        $http
        .post("https://9er.firebaseio.com/planets/.json", planet)
        .success(
        function (data) {
            planet.key = data.name;
            var location = (Planets.push(planet))-1;
            $location.path("/"+location);
        })
    .error(
    function () {
        console.log("Error");
    });
    };
});
app.controller("editPlanetController", function ($scope, $http, Planets){

});
app.controller("listPlanetController", function (Planets, $scope, $http){
    $scope.planets = Planets;
});